/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 */
package com.apache.order.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.order.entity.OrderLogMessage;
import com.apache.order.manager.OrderLogMessageManager;

/**
 * description:  订单引擎操作记录
 * @author Hou Dayu 创建时间：2015-8-14  
 */
public class OrderLogMessageManagerImpl implements OrderLogMessageManager {

	protected IDao orderLogMessageDao;
	protected final String entityName = "com.dwuwusoft.order.entity.OrderLogMessage";

	public void setOrderLogMessageDao(IDao orderLogMessageDao) {
		this.orderLogMessageDao = orderLogMessageDao;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.order.manager.UnityBaseManager#getPageInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Page getPageInfo(ParamsVo<OrderLogMessage> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderby = Validator.getDefaultStr(String.valueOf(vo.getParams("orderby")), "created_time desc");
		param.setParams("orderBy", orderby);
		Page page = orderLogMessageDao.pageSelect(param);
		return page;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#countInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public long countInfo(ParamsVo<OrderLogMessage> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return orderLogMessageDao.count(param);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#deleteInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public boolean deleteInfo(ParamsVo<OrderLogMessage> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		OrderLogMessage info = (OrderLogMessage) orderLogMessageDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("OrderLogMessage");
			return orderLogMessageDao.edit(param);
		} else {
			param.setParams("logId", infoId);
			param.setDelete(true);
			return orderLogMessageDao.delete(param);
		}
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#editInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public boolean editInfo(ParamsVo<OrderLogMessage> vo) throws BusinessException {
		//		OrderLogMessage logMessage = vo.getObj();
		//		if (Validator.isNotNull(logMessage.getLogId())) {
		//			String key = Validator.getDefaultStr(vo.getMethodKey(), "OrderLogMessage");
		//			String cacheKey = "";
		//			MethodParam param = new MethodParam(key, cacheKey, "", entityName);
		//			param.setVaule(logMessage);
		//			return orderLogMessageDao.edit(param);
		//		}
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#execute(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo<OrderLogMessage> arg0) {

		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#getInfoById(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Object getInfoById(ParamsVo<OrderLogMessage> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return orderLogMessageDao.selectById(param);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#getList(com.dwusoft.api.vo.ParamsVo)  
	 */
	public List<OrderLogMessage> getList(ParamsVo<OrderLogMessage> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderby = Validator.getDefaultStr(String.valueOf(vo.getParams("orderby")), "created_time desc");
		param.setParams("orderBy", orderby);
		return orderLogMessageDao.select(param);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#saveInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public String saveInfo(ParamsVo<OrderLogMessage> vo) throws BusinessException {
		OrderLogMessage logMessage = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		logMessage.setLogId(infoId);
		MethodParam param = new MethodParam("OrderLogMessage", cacheKey, "", entityName);
		param.setVaule(logMessage);
		if (orderLogMessageDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	private MethodParam setMethodParams(ParamsVo<OrderLogMessage> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(orderLogMessageDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		OrderLogMessage logMessage = vo.getObj();
		if (Validator.isEmpty(logMessage))
			return param;
		if (Validator.isNotNull(logMessage.getObjId())) {
			sb.append(" and objId = :objId");
			param.setParams("objId", logMessage.getObjId());
		}
		if (Validator.isNotNull(logMessage.getUserEname())) {
			sb.append(" and userEname = :userEname");
			param.setParams("userEname", logMessage.getUserEname());
		}
		if (Validator.isNotNull(logMessage.getLogType())) {
			sb.append(" and logType = :logType");
			param.setParams("logType", logMessage.getLogType());
		}

		if (Validator.isNotNull(logMessage.getReserved2())) {
			sb.append(" and reserved2 = :reserved2");
			param.setParams("reserved2", logMessage.getReserved2());
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
